﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using slava.dao;

namespace DAO
{
    public class EmployeeRepository : IDao<Employer>
    {
        skladEntities sklad;
        public EmployeeRepository()
        {
            sklad = new skladEntities();
        }

        public void Add(Employer entity)
        {
            sklad.Employers.Add(entity);
            sklad.SaveChanges();
        }

        public void Delete(int id)
        {
            Employer empl = sklad.Employers.FirstOrDefault(t => t.Id == id);
            sklad.Employers.Remove(empl);
            sklad.SaveChanges();
        }

        public Employer GetById(int id)
        {
            return sklad.Employers.FirstOrDefault(t => t.Id == id);
        }

        public IEnumerable<Employer> GetAll()
        {
            return sklad.Employers;
        }

        public void Edit(Employer entity)
        {
            var temp = sklad.Employers.Single(x => x.Id == entity.Id);
            sklad.Entry(temp).CurrentValues.SetValues(entity);
            sklad.SaveChanges(); 
        }

        public bool IsUserValid(string login, string passrord)
        {
            Employer empl = sklad.Employers.FirstOrDefault(t => t.Login == login);
            if (empl == null)
                return false;
            if (empl.Password != passrord)
                return false;
            return true;
        }

        public bool? IsAdmin(string login)
        {
            Employer empl = sklad.Employers.FirstOrDefault(t => t.Login == login);
            return empl.Is_Admin;
        }
    }
}
